/*
 * This file is part of b.AMP.
 *
 * b.AMP is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * b.AMP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with b.AMP.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __BAMP_PLUGIN_LOADER_HPP__
#define __BAMP_PLUGIN_LOADER_HPP__

#include <bamp/noncopyable.hpp>
#include <boost/filesystem.hpp>
#include <memory>

namespace bamp
{

    /**
     * A plugin is just a class in a dynamic library with a create()
     * function. A plugin loader loads plugins of a specific class. It
     * also maintains a mapping of the data types supported by plugins
     * and provides a way to create plugin instances.
     */
    class PluginLoader : bamp::Noncopyable
    {
    public:

        typedef boost::filesystem::path Path;
        
        // ctor/dctor
        PluginLoader();
        ~PluginLoader();

        // Global load/unload
        void load(const Path& path, int priority);
        void unload(const Path& path);

        // Plugin access
        template<typename T>
        T create(const Path& path) const
        {
            return (typename T::ctype*)_create(path, T::cname);
        }

        template<typename T>
        Path find(const std::string& type) const
        {
            return _find(type, T::cname);
        }


    private:

        // Internal, non-template methods
        void* _create(const Path& path,
                      const std::string& classname) const;
        Path _find(const std::string& type,
                   const std::string& classname) const;

        // Hidden implementation details
        struct Implementation;
        std::auto_ptr<Implementation> pimpl;
    };

} // namespace bamp


#endif // __BAMP_PLUGIN_LOADER_HPP__
